package org.cloud.utils.image;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.cloud.vo.ValidateCodeVO;

public final class ValidateCodeUtil {

    public static int defaultWidth = 200;     //图片宽度
    public static int defaultHeight = 100;    //图片高度
    public static int defaultStringNum = 4;  //字符的数量
    public static int defaultLineSize = 10;  //干扰线数量

    private ValidateCodeUtil() {
    }

    private final static ValidateCodeUtil instance = new ValidateCodeUtil();

    public static ValidateCodeUtil getInstance() {
        return instance;
    }

    /**
     * @return String 返回base64
     * @MethodName: getRandomCode
     * @Description: 生成Base64图片验证码
     */
    public ValidateCodeVO getRandomCode(final int width, final int height, final int lineSize, final int StringNum) {
        ValidateCodeVO validate = new ValidateCodeVO();
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(width, height, StringNum, lineSize);
        validate.setBase64ImageUrl("data:image/png;base64," + lineCaptcha.getImageBase64());
        validate.setValue(lineCaptcha.getCode());
        return validate;
    }

    public ValidateCodeVO getRandomCode() {
        return getRandomCode(defaultWidth, defaultHeight, defaultLineSize, defaultStringNum);
    }
}
